home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 34.zip / BS1 part 34 / FredFish PD 314.adf / Zc / zcsrc.lzh / IOLib / stdio / printf.c < prev    next >
C/C++ Source or Header  |  1989-05-28  |  3KB  |  175 lines

  1. #include <stdio.h>
  2. #include <ctype.h>
  3.  
  4. #define    FALSE 0
  5. #define TRUE (!FALSE)
  6.  
  7. _printf(op, put, fmt, args)
  8.     char *op;
  9.     unsigned int (*put)();
  10.     register unsigned char *fmt;
  11.     register unsigned int *args;
  12.     {
  13.     register int i, cnt = 0, ljustf, lval;
  14.     int preci, dpoint, width;
  15.     char pad, sign, radix;
  16.     register char *ptmp;
  17.     char tmp[64], *ltoa(), *ultoa();
  18. #ifdef FLOATS
  19.     double fx;
  20. #endif
  21.  
  22.     while(*fmt)
  23.         {
  24.         if(*fmt == '%')
  25.             {
  26.             ljustf = FALSE;    /* left justify flag */
  27.             sign = '\0';    /* sign char & status */
  28.             pad = ' ';    /* justification padding char */
  29.             width = -1;    /* min field width */
  30.             dpoint = FALSE;    /* found decimal point */
  31.             preci = -1;    /* max data width */
  32.             radix = 10;    /* number base */
  33.             ptmp = tmp;    /* pointer to area to print */
  34.             lval = FALSE;    /* long value flaged */
  35. fmtnxt:
  36.             i = 0;
  37.             while (isdigit(*++fmt))
  38.                 {
  39.                 i = (i * 10) + (*fmt - '0');
  40.                 if (dpoint)
  41.                     preci = i;
  42.                 else if (!i && (pad == ' '))
  43.                     {
  44.                     pad = '0';
  45.                     goto fmtnxt;
  46.                     }
  47.                 else
  48.                     width = i;
  49.                 }
  50.  
  51.             switch(*fmt)
  52.                 {
  53.                 case '\0':    /* early EOS */
  54.                     --fmt;
  55.                     goto charout;
  56.  
  57.                 case '-':    /* left justification */
  58.                     ljustf = TRUE;
  59.                     goto fmtnxt;
  60.  
  61.                 case ' ':
  62.                 case '+':    /* leading sign flag */
  63.                     sign = *fmt;
  64.                     goto fmtnxt;
  65.  
  66.                 case '*':    /* parameter width value */
  67.                     i = *args++;
  68.                     if (dpoint)
  69.                         preci = i;
  70.                     else
  71.                         width = i;
  72.                     goto fmtnxt;
  73.  
  74.                 case '.':    /* secondary width field */
  75.                     dpoint = TRUE;
  76.                     goto fmtnxt;
  77.  
  78.                 case 'l':    /* long data */
  79.                     lval = TRUE;
  80.                     goto fmtnxt;
  81.  
  82.                 case 'd':    /* Signed decimal */
  83.                 case 'i':
  84.                     ltoa((long)((lval)
  85.                         ?(*((long *) args))
  86.                         :(*((int  *) args))),
  87.                           ptmp, 10);
  88.                     if(lval)
  89.                         args = ((unsigned int *)
  90.                             (((long *) args) + 1));
  91.                     else
  92.                         args = ((unsigned int *)
  93.                             (((int *) args) + 1));
  94.                     goto printit;
  95.  
  96.                 case 'b':    /* Unsigned binary */
  97.                     radix = 2;
  98.                     goto usproc;
  99.  
  100.                 case 'o':    /* Unsigned octal */
  101.                     radix = 8;
  102.                     goto usproc;
  103.  
  104.                 case 'p':    /* Pointer */
  105.                     lval = TRUE;
  106.                     pad = '0';
  107.                     width = 6;
  108.                     preci = 8;
  109.                     /* fall thru */
  110.  
  111.                 case 'x':    /* Unsigned hexadecimal */
  112.                 case 'X':
  113.                     radix = 16;
  114.                     /* fall thru */
  115.  
  116.                 case 'u':    /* Unsigned decimal */
  117. usproc:
  118.                     ultoa((unsigned long)((lval)
  119.                         ?(*((unsigned long *) args))
  120.                         : *args++ ),
  121.                           ptmp, radix);
  122.                     if(lval)
  123.                         args = ((unsigned int *)
  124.                         (((unsigned long *) args) + 1));
  125.                     if (*fmt == 'x')
  126.                         strlwr(ptmp, ptmp);
  127.                     goto printit;
  128.  
  129. #ifdef FLOATS
  130.                 case 'e':    /* float */
  131.                 case 'f':
  132.                 case 'g':
  133.                 case 'E':
  134.                 case 'G':
  135.                     fx = *((double *) args);
  136.                     args=(unsigned int *)
  137.                          (((double *) args)+1);
  138.  
  139.                     fp_print(fx, *fmt, preci, ptmp);
  140.                     preci = -1;
  141.                     goto printit;
  142. #endif
  143.  
  144.                 case 'c':    /* Character */
  145.                     ptmp[0] = *args++;
  146.                     ptmp[1] = '\0';
  147.                     goto nopad;
  148.  
  149.                 case 's':    /* String */
  150.                     ptmp = *((char **) args);
  151.                     args = ((unsigned int *)
  152.                         (((char **) args) + 1));
  153. nopad:
  154.                     sign = '\0';
  155.                     pad  = ' ';
  156. printit:
  157.                     cnt += _prtfld(op, put, ptmp, ljustf,
  158.                                sign, pad, width, preci);
  159.                     break;
  160.  
  161.                 default:    /* unknown character */
  162.                     goto charout;
  163.                 }
  164.             }
  165.         else
  166.             {
  167. charout:
  168.             (*put)(*fmt, op);        /* normal char out */
  169.             ++cnt;
  170.             }
  171.         ++fmt;
  172.         }
  173.     return(cnt);
  174.     }
  175.